﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using DflyingForum.Core.Framework;

namespace Core.Web.Code.UI.Controls.ChildControls
{
    /// <summary>
    /// 面包屑导航
    /// </summary>
    public partial class BreadNavigator : BaseControl
    {
        protected PlaceHolder BreadPlaceHolder;

        private static readonly string SEP_STR = " > ";
        private static readonly string MODULE_URL = "~/Thread.aspx?mid={0}";//子版块
        private static readonly string MODULE_POST_URL = "~/post.aspx?mid={0}&pid={1}";//帖子查看页

        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlAnchor Home = new HtmlAnchor();
            Home.HRef = "~/Default.aspx";
            Home.InnerText = "论坛主页";
            Home.Attributes.Add("class", "home");

            BreadPlaceHolder.Controls.Add(Home);

            int mid = Query.Request("mid", 0);
            if (mid > 0)
            {
                var module = _bllModule.FindBy(mid);
                if (module == null)//当然要用错误页机制了,这里简单处理下
                    return;
                BreadPlaceHolder.Controls.Add(GetSepControl());
                HtmlAnchor Module = new HtmlAnchor { InnerText = module.Name, HRef = string.Format(MODULE_URL, mid) };
                BreadPlaceHolder.Controls.Add(Module);
            }
            int pid = Query.Request("pid", 0);
            if (mid > 0 && pid > 0)
            {
                var post = _bllPost.FindBy(pid);
                if (post == null)//当然要用错误页机制了,这里简单处理下
                    return;
                BreadPlaceHolder.Controls.Add(GetSepControl());
                HtmlAnchor Module = new HtmlAnchor { InnerText = post.Subject, HRef = string.Format(MODULE_POST_URL, mid, pid) };
                BreadPlaceHolder.Controls.Add(Module);
            }
        }

        private Control GetSepControl()
        {
            HtmlGenericControl control = new HtmlGenericControl();
            control.InnerText = SEP_STR;
            return control;
        }
    }
}